package com.dz.service.impl;

import com.dz.bean.Guide;
import com.dz.bean.Manager;
import com.dz.bean.Visitor;
import com.dz.mapper.GuideDao;
import com.dz.mapper.ManagerDao;
import com.dz.mapper.VisitorDao;
import com.dz.service.LoginService;
import com.dz.util.PasswordUtil;
import com.dz.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dz.util.SYS;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    ManagerDao managerDao;
    @Autowired
    VisitorDao visitorDao;
    @Autowired
    GuideDao guideDao;

    @Override
    public String managerLogin(Manager manager) {
        //1.登录逻辑
        Manager result = managerDao.getByUsername(manager.getUsername());
        if (result == null){
            throw new RuntimeException("登录异常");
        }
        //2.验证密码
        String password = manager.getPassword();
        String encryptionPassword = result.getPassword();

        if (!PasswordUtil.checkPassword(password,encryptionPassword)){
            throw new RuntimeException("登录异常");
        }
        //3.返回结果token
        String token = TokenUtil.getToken(result.getId(), result.getUsername(), "manager");
        return token;
    }

    @Override
    public String visitorLogin(Visitor visitor) {
        //1.登录逻辑
        Visitor result = visitorDao.getByUsername(visitor.getUsername());
        if (result == null){
            throw new RuntimeException("登录异常");
        }
        //2.验证密码
        String password = visitor.getPassword();
        String encryptionPassword = result.getPassword();

        if (!PasswordUtil.checkPassword(password,encryptionPassword)){
            throw new RuntimeException("登录异常");
        }
        //3.返回结果token
        String token = TokenUtil.getToken(result.getId(),result.getUsername(),"visitor");
        return token;
    }

    @Override
    public String guideLogin(Guide guide) {
        //1.登录逻辑
        Guide result = guideDao.getByUsername(guide.getUsername());
        if (result == null){
            throw new RuntimeException("登录异常");
        }
        //2.验证密码
        String password = guide.getPassword();
        String encryptionPassword = result.getPassword();

        if (!PasswordUtil.checkPassword(password,encryptionPassword)){
            throw new RuntimeException("登录异常");
        }
        //3.返回结果token
        String token = TokenUtil.getToken(result.getId(), result.getUsername(),"guide");
        return token;
    }

    @Override
    public Manager mLoginInfo(String token){
        Object role = TokenUtil.getOption(token, SYS.TOKEN_ROLE);
        if(role==null||!SYS.ROLE_MANAGER

                .equals(role.toString())){
            throw new RuntimeException("角色异常");
        }

        Object id = TokenUtil.getOption(token, SYS.TOKEN_ID);
        if (id==null){
            throw new RuntimeException("登录异常");
        }
        Manager manager= managerDao.findById(Integer.parseInt(id.toString()));
        return manager;
    }

    @Override
    public Visitor vLoginInfo(String token){
        Object role = TokenUtil.getOption(token, SYS.TOKEN_ROLE);
        if(role==null||!SYS.ROLE_VISITOR.equals(role.toString())){
            throw new RuntimeException("角色异常");
        }

        Object id = TokenUtil.getOption(token, SYS.TOKEN_ID);
        if (id==null){
            throw new RuntimeException("登录异常");
        }
        Visitor visitor= visitorDao.findById(Integer.parseInt(id.toString()));
        return visitor;
    }

    @Override
    public Guide gLoginInfo(String token){
        Object role = TokenUtil.getOption(token, SYS.TOKEN_ROLE);
        if(role==null||!SYS.ROLE_GUIDE.equals(role.toString())){
            throw new RuntimeException("角色异常");
        }

        Object id = TokenUtil.getOption(token, SYS.TOKEN_ID);
        if (id==null){
            throw new RuntimeException("登录异常");
        }
        Guide guide = guideDao.findById(Integer.parseInt(id.toString()));
        return guide;
    }
}
